/**
 * 
 */
package cn.egame.extraction.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.extraction.pojo.EfsInfo;

/**
 * Description 程序初始化启动
 * 
 * @ClassName ExtractionInit
 * 
 * @Copyright 炫彩互动
 * 
 * @Project egame.mobile.extraction
 * 
 * @Author Administrator
 * 
 * @Create Date 2014-6-12
 * 
 * @Modified by none
 * 
 * @Modified Date
 */

public class ExtractionInit {

	private static final Logger logger = Logger.getLogger(ExtractionInit.class);
	// 文件处理list
	public static List<EfsInfo> efsInfoList = new ArrayList<EfsInfo>();
	public static long readMaxId=0;
    public static long writeMaxId=0;
    public static int count =0;
    public static String dayStr="2014-07-07";
	/**
	 * @Description: 程序初始化加载数据
	 * 
	 * @return void
	 * @throws
	 * @Author yujm
	 * @Create Date 2014-6-9
	 * @Modified by none
	 * @Modified Date
	 */
	public static void init() {
		
		String dayStr;
		try {
			// 清空list
			efsInfoList.clear();
			// System.out.println("efsInfoList size : "+efsInfoList.size());
			File file = new File(ConstVar_EFS.MAX_ID_FILE_PATH);
			File dayFile = new File(ConstVar_EFS.DAY_FILE_PATH);
			if (!file.exists() || !dayFile.exists()) {
				throw new ExceptionCommonBase(-1, "存储最大id的文件或存储日期的文件不存在");
			}

			BufferedReader br = null;
			BufferedReader br2 = null;
			try {
				br = new BufferedReader(new InputStreamReader(
						new FileInputStream(file), "utf-8"));
				br2 = new BufferedReader(new InputStreamReader(
						new FileInputStream(dayFile), "utf-8"));
				readMaxId = new Long(br.readLine());
				dayStr = br2.readLine();
			} catch (Exception e) {
				logger.error(e);
				throw new ExceptionCommonBase(-1, "从文件中读取id失败");
			} finally {
				if (br != null) {
					try {
						br.close();
					} catch (IOException e) {
						logger.error(e);
						throw ExceptionCommonBase.throwExceptionCommonBase(e);
					}
				}
				if (br2 != null) {
					try {
						br2.close();
					} catch (IOException e) {
						logger.error(e);
						throw ExceptionCommonBase.throwExceptionCommonBase(e);
					}
				}
			}
			if (readMaxId == 0L) {
				throw new ExceptionCommonBase(-1, "文件中id为0");
			}
			if (dayStr == null || !dayStr.matches("^\\d+-\\d+-\\d+$")) {
				throw new ExceptionCommonBase(-1, "文件中日期不存在或日期格式不符合需求");
			}
			// 从拉取失败的文件中填充重新需要拉取的id
			if (ConstVar_EFS.IS_INIT) {

				FileService.fillEfsInfoListFromErrorFile(efsInfoList);

				ConstVar_EFS.IS_INIT = false;
			}
			// 路径是由日期生成的文件解析
			if (ConstVar_EFS.DAY_FILE_PATH != null) {
				String[] monthFileTypes = ConstVar_EFS.DAY_FILE_TYPE.split(",");
				for (String fileType : monthFileTypes) {
					String url2 = ConstVar_EFS.MAX_ID_URL
							+ FileService.contactUrl("efs_id=" + readMaxId,
									"efs_type=" + fileType, "num=0", "day="
											+ dayStr);
					logger.info(url2);
					writeMaxId = FileService.parseMaxIdUrlInfo(url2,
							efsInfoList);
				}
			}
			// 路径是由efs生成的文件解析
			String url = ConstVar_EFS.MAX_ID_URL
					+ FileService.contactUrl("efs_id=" +readMaxId, "efs_type="
							+ ConstVar_EFS.EFS_TYPE, "num=" + ConstVar_EFS.NUM,
							"end_id=" + ConstVar_EFS.END_ID);
			logger.info(url);
			writeMaxId = FileService.parseMaxIdUrlInfo(url, efsInfoList);
			logger.info("#当前要处理的任务个数："+efsInfoList.size());
			if (writeMaxId < readMaxId) {
				throw new ExceptionCommonBase(-1, "返回的maxId小于读取的maxId");
			}
			// System.out.println("readMaxId:"
			// +readMaxId+" writerMaxid: "+writeMaxId);

			//writerBack(writeMaxId, readMaxId, dayStr);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

	}

	/**
	 * @Description: 下一次读取的位置回写
	 * 
	 * @return void
	 * @throws
	 * @Author yujm
	 * @Create Date 2014-6-10
	 * @Modified by none
	 * @Modified Date
	 */
	public static void writerBack(long writeMaxId, long readMaxId, String dayStr) {
		try {
			FileService.storeIdToFile(writeMaxId,
					ConstVar_EFS.MAX_ID_FILE_PATH, readMaxId, writeMaxId);
		} catch (ExceptionCommonBase e) {

			logger.error(e.getMessage(), e);
		}
		FileService.storeDayToFile(dayStr);
	}
}
